﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Security;

namespace iLangl
{
	public abstract class BaseSecurityAction : BaseAction
	{

		protected void raiseAlert(Exception ex)
		{
			IAlert alert = Facade.Instance.AlertManager.CreateAlert(this, ex);
			alert.AddAction(this);
			Facade.Instance.AlertManager.RaiseAlert(alert);
		}

		protected override string GetActionId()
		{
			return Guid.NewGuid().ToString();
		}

		public override void SetActionDescriptor(IActionDescriptor descriptor)
		{
			base.SetActionDescriptor(descriptor);
			SessionId = descriptor.Container.SessionId;
			PluginLiceneKey = descriptor.Container.PluginLiceneKey;
			ContactId = descriptor.Container.ContactId;
			CompanyId = descriptor.Container.CompanyId;
			IsSynced = descriptor.IsSynced;
		}


		protected override void validate()
		{
			if (String.IsNullOrEmpty(SessionId)
				|| String.IsNullOrEmpty(PluginLiceneKey)
				|| String.IsNullOrEmpty(ContactId)
				|| String.IsNullOrEmpty(CompanyId)
				)
			{

				SecurityException exception = new SecurityException("Security Action Authorization Failed!");
				registerException(exception);
			}
		}
	}
}
